package satir.analysis.analyst;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * The Class Factor.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class Factor {

    /** The key. */
    private String key;

    /** The name. */
    private String name;

    /** The interpretation. */
    private String interpretation;

    /** The advice. */
    private ArrayList<Advice> advice;

    /**
     * Instantiates a new factor.
     * @param json
     *         the JSON object
     */
    public Factor(final JSONObject json) {
	advice = new ArrayList<Advice>();
	try {
	    key = json.getString("key");
	    interpretation = json.getString("interpretation");
	    name = json.getString("name");
	    JSONArray advice = json.getJSONArray("advice");
	    for (int i = 0; i < advice.length(); i++) {
		JSONObject current = advice.getJSONObject(i);
		Integer rank = current.getInt("rank");
		String string = current.getString("advice");
		this.advice.add(new Advice(rank, string));
	    }
	} catch (JSONException e) {
	    e.printStackTrace();
	}
    }

    /**
     * Gets the advice.
     * @param rank
     *		the rank
     * @return the advice
     */
    public String getAdvice(final int rank) {
	String feeback = "";
	for (int i = 0; i < advice.size(); i++) {
	    Advice current = advice.get(i);
	    if (rank >= current.getRank()) {
		feeback = current.getAdvice();
	    } else {
		break;
	    }
	}
	return feeback;
    }

    /**
     * Sort advice by rank.
     */
    public void sortAdviceByRank() {
	for (int i = 0; i < advice.size(); i++) {
	    Advice first = advice.get(i);
	    for (int j = 0; j < advice.size(); j++) {
		Advice second = advice.get(i);
		if (first.compareTo(second) > 0) {
		    advice.set(i, second);
		    advice.set(j, first);
		}
	    }
	}
    }

    /**
     * Gets the interpretation.
     * @return the interpretation
     */
    public String getInterpretation() {
	return interpretation;
    }

    /**
     * Gets the name.
     * @return the name
     */
    public String getName() {
	return name;
    }

    /**
     * Gets the key.
     * @return the key
     */
    public String getKey() {
	return key;
    }

    /**
     * The Class Advice.
     */
    public class Advice implements Comparable {

	/** The rank. */
	private int rank;

	/** The advice. */
	private String advice;

	/**
	 * Instantiates a new advice.
	 * @param rank
	 *		the rank
	 * @param advice
	 *		the advice
	 */
	public Advice(final int rank, final String advice) {
	    this.rank = rank;
	    this.advice = advice;
	}

	/**
	 * Gets the rank.
	 * @return the rank
	 */
	public int getRank() {
	    return rank;
	}

	/**
	 * Gets the advice.
	 * @return the advice
	 */
	public String getAdvice() {
	    return advice;
	}
        /**
	 * Compare the ranking.
	 * @param advice
	 *        the advice detail
	 * @return a negative integer, zero, or a positive integer
	 */
	@Override
	public int compareTo(final Object advice) {
	    int value = rank - ((Advice) advice).getRank();
	    return value;
	}
    }
}
